package org.example;

//import net.sf.jxls.transformer.XLSTransformer;
import cn.afterturn.easypoi.util.PoiMergeCellUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.example.POJO.ApprovalBO;
import org.example.POJO.OfferFormBO;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        try {
//            ClassPathResource resource = new ClassPathResource("1.xls");
            // 读取模板
            InputStream is = App.class.getClassLoader().getResourceAsStream("2.xls");
            // 新建文件
            File file = new File("d:/333.xls");
            // 流
            OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
            //ByteArrayOutputStream os = new ByteArrayOutputStream();
            // 定义数据  使用Context类型
            Context context = new Context();

            context.putVar("personCID", "111");
            context.putVar("name", "222");
            context.putVar("jobName", "挖掘机");

            List<ApprovalBO> approvalBOList = new ArrayList<>();
            ApprovalBO approvalBO1 = new ApprovalBO();
            ApprovalBO approvalBO2 = new ApprovalBO();
            ApprovalBO approvalBO3 = new ApprovalBO();
            ApprovalBO approvalBO4 = new ApprovalBO();

            approvalBO1.setName("审批人");
            approvalBO1.setEmail("邮箱");
            approvalBO1.setTime("审批时间");
            approvalBO1.setOptition("审批意见");

            approvalBO2.setName("张哈哈");
            approvalBO2.setEmail("123@456");
            approvalBO2.setTime("2099-10-32 18:00");
            approvalBO2.setOptition("同意");

            approvalBO3.setName("张笑笑");
            approvalBO3.setEmail("123@456");
            approvalBO3.setTime("2099-10-32 19:00");
            approvalBO3.setOptition("不同意");

            approvalBO4.setName("张哭哭");
            approvalBO4.setEmail("123@456");
            approvalBO4.setTime("2099-10-32 20:00");
            approvalBO4.setOptition("再考虑");

            approvalBOList.add(approvalBO1);
            approvalBOList.add(approvalBO2);
            approvalBOList.add(approvalBO3);
            approvalBOList.add(approvalBO4);

            List<OfferFormBO> offerFormBOS = new ArrayList<>();
            OfferFormBO offerFormBO = new OfferFormBO();
            offerFormBO.setApprovalBOList(approvalBOList);
            offerFormBOS.add(offerFormBO);
            context.putVar("offerFormBOS", offerFormBOS);

            System.out.println(context);

            // 数据写入模板
            JxlsHelper.getInstance().processTemplate(is, os, context);

//            os.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * @description:  合并单元格
     * @author: zhangfuzhena
     * @date: 2022/9/26 11:05
     * @param: [sheet, resume]
     * @return: void
     **/
    public static void employeeResumeMergeCell(Sheet sheet, Context resume){
        //合并家庭主要成员单元格
//        List<EmployeeResumeFamilyBO> familys = resume.getFamilys();
        int startRow = 6;
        int endRow = 7;
        int resumeSize = 3;
        int familyOffset = resumeSize == 0 ? 0 : resumeSize - 1;
        //合并行
        mergeRowCell(sheet, resumeSize, endRow, 1, 2);
        //合并列
//        PoiMergeCellUtil.addMergedRegion(sheet, familyStartRow, familyEndRow + familyOffset, 0, 0);


    }

    private static void mergeRowCell(Sheet sheet, int size, int row, int startCol, int endCol) {
        //合并行
        if (size == 0) {
            //如果为空
            PoiMergeCellUtil.addMergedRegion(sheet, row, row, startCol, endCol);
        } else {
            //如果不为空
            for (int i = 0; i < size; i++) {
                PoiMergeCellUtil.addMergedRegion(sheet, row, row, startCol, endCol);
                row++;
            }
        }
    }
}
